package edu.rmit.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;

/**
 * This panel is used to input message on the live chat and forum window.
 * 
 * @author LiYazhou
 * 
 */
public class MessageEditorPanel extends JPanel {

	private static final long serialVersionUID = 5286573318008741310L;

	/**
	 * The list which display the message.
	 */
	private JList listMessage;

	/**
	 * The text area which display the message which user want to send
	 */
	private JTextArea taEditor;

	/**
	 * The button which will canuse the text will be send when a user clicked
	 * this.
	 */
	private JButton buttonSend;

	/**
	 * This method is to add a action listener to a particular button.
	 * 
	 * @param actionListener
	 *            The action listener which implement the logic when a user
	 *            clicked this.
	 */
	public void setListener(ActionListener actionListener) {
		buttonSend.addActionListener(actionListener);
	}

	/**
	 * This method is get the text area which display the message which user
	 * want to send it to other person.
	 * 
	 * @return The text area which contains the message to be send to other.
	 */
	public JTextArea getTaEditor() {
		return taEditor;
	}

	/**
	 * Creates the components.
	 */
	public MessageEditorPanel() {
		this.setLayout(new BorderLayout());
		setSize(200, 400);
		setBackground(Color.white);
		this.setBorder(BorderFactory.createTitledBorder("Message"));

		listMessage = new JList();
		listMessage.setVisibleRowCount(10);
		// listMessage.setSize(200, 400);
		listMessage.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listMessage.setFixedCellHeight(20);
		listMessage.setFixedCellWidth(200);

		// Change the colors of the lists
		listMessage.setBackground(Color.white);
		listMessage.setForeground(Color.black);

		// Add lists to the scrollbar containers
		add(new JScrollPane(listMessage, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);

		taEditor = new JTextArea("");
		taEditor.setRows(5);
		taEditor.setColumns(20);
		taEditor.setToolTipText("Input message here");
		taEditor.setEditable(true);
		taEditor.setLineWrap(true);

		JScrollPane jsEditor = new JScrollPane(taEditor,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		buttonSend = new JButton("Send");
		JPanel panelEditor = new JPanel();
		panelEditor.setLayout(new BorderLayout());
		panelEditor.add(jsEditor, BorderLayout.CENTER);
		panelEditor.add(buttonSend, BorderLayout.EAST);
		panelEditor.setBorder(BorderFactory
				.createTitledBorder("Input message here"));

		add(panelEditor, BorderLayout.SOUTH);
	}

	/**
	 * This method is to set the panel border for the current panel.
	 * 
	 * @param title
	 *            The title which you want to be setted to this panel.
	 */
	public void setTitle(String title) {
		this.setBorder(BorderFactory.createTitledBorder(title));
	}

	/**
	 * This method is to get the message list control
	 * @return The message list control.
	 */
	public JList getListMessage() {
		return listMessage;
	}
}
